const util = require("../../../utils/util")
const domainName = getApp().globalData.domainName
const header = {'content-type':'application/x-www-form-urlencoded'}
const dataType = 'json'
const method = 'POST'
const responseType = 'text'
// pages/certified_users/certified_users.js
Page({
  dis(){},
  /**
   * 页面的初始数据
   */
  data: {
    user_list:[],
    type_list:[
      {type_no:'', type_name:'全部'},
      {type_no:1, type_name:'已持证'},
    ],
  },


  set_label(e){
    if(!this.data.off_vibration){
      getApp().get_vibration()
    }
    let index = e.currentTarget.id
    this.setData({
      label_id:index,
      no_more:false,
    })
    if(index==1){ // 全部
      var begin = '2012-12-12'
      var end = util.formatDate(new Date())
    }else if(index==2){ // 今天
      let today = util.formatDate(new Date())
      var begin = today
      var end = today
    }else if(index==3){ // 昨天
      // console.log('end',end)
      let the_day = new Date()
      the_day.setDate(the_day.getDate()-1)
      var begin = util.formatDate(the_day)
      var end = begin
    }else if(index==4){ // 最近30月
      var end = util.formatDate(new Date())
      // console.log('end',end)
      let the_day = new Date()
      the_day.setMonth(the_day.getMonth()-1)
      var begin = util.formatDate(the_day)
    }else if(index==5){ // 最近半年
      var end = util.formatDate(new Date())
      // console.log('end',end)
      let the_day = new Date()
      the_day.setMonth(the_day.getMonth()-6)
      var begin = util.formatDate(the_day)
    }
    this.start_date = begin
    this.end_date = end
    this.load_certified_users(0, this.type_no, this.data.search_word, this.start_date, this.end_date)

  },

  cancel(){
    this.onLoad(this.options)
  },

  go_search(){
    this.setData({
      type_index:0,
      label_id:1,
      no_more:false,
    })
    this.type_no = ''
    this.page_no = 0
    this.start_date = ''
    this.end_date = ''
    this.load_certified_users(0, '', this.data.search_word, '', '')
  },

  input_search(e){
    let search_word = e.detail.value
    this.setData({
      search_word:search_word,
    })
  },

  type_name(e){
    let index = e.detail.value
    let type_no = this.data.type_list[index].type_no
    this.setData({
      type_index:index,
      no_more:false,
    })
    this.type_no = type_no
    this.page_no = 0
    this.load_certified_users(0, type_no, this.data.search_word, this.start_date, this.end_date)
  },

  view_user(e){
    let index = e.currentTarget.id
    let sel_user = this.data.user_list[index].user_id
    wx.navigateTo({
      url: '/pages/my_paper_index/my_paper_index?user_id=' + sel_user,
    })


  },

  load_certified_users(page_no, type_no, search_word, start_date, end_date){
    wx.request({
      url: domainName + '/load_certified_users',
      data: {
        page_no:page_no,
        type_no:type_no,
        search_word:search_word,
        start_date:start_date,
        end_date:end_date,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        let user_list = res.data.user_list
        if(page_no){
          this.setData({
            user_list: this.data.user_list.concat(user_list),
          })
        }else{
          this.setData({
            user_list:user_list,
            certified_count:res.data.certified_count,
          })
        }
        this.page_no += 1
        if(user_list.length==0){
          this.setData({
            no_more:true
          })
        }

      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type_index:0,
      search_word:'',
      label_id:1,
    })
    this.page_no = 0
    this.type_no = '' 
    this.start_date = ''
    this.end_date = ''
    this.load_certified_users(0,this.type_no,'', this.start_date, this.end_date)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.no_more){
      this.load_certified_users(this.page_no, this.type_no, this.data.search_word, this.start_date, this.end_date)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})